<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of "myWiWall".
# Copyright (c) 2007-2008 CRP Henri Tudor and contributors.
# All rights reserved.
#
# "myWiWall" is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# 
# "myWiWall" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with "myWiWall"; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ***** END LICENSE BLOCK *****

/**
 * widget resource class
 */
class res_tag implements RESTResource {
	
 	/**
 	 * extension asked
 	 */	
	private $ext;
	
 	/**
 	 * reference to the request object
 	 */	
	private	$request;
	
	
	/**
	 * authenticated user
	 */
	private $userid;
	
	/**
	 * authenticated user level
	 */	
	private $userlevel;

	// FIXME extension pour Palette
	private $app;

 	/**
 	 * Constructor
 	 * @param	HttpRequest		$pReq			request
 	 * @param	string			$pExt			extension
 	 * @param	array			$pConstraints	list of constraints on this resource
 	 */
    public function __construct(RESTHttpRequest $pReq, $pExt = null, $pConstraints = array()) 
    {
    	$this->request = $pReq;
    	$this->ext = $pExt;    	
    }
    
 	/**
 	 * Tests if a string is an id for this resource class (this method is optional)
 	 * @param	string	$pInputStr	the string tested
 	 * @return	integer	0 for false, and 1 for true (not boolean because this method is called by call_user_func)
 	 */
    public static function isId($pInputStr) 
    {
    	return 1;
    }

 	/**
 	 * authentication
 	 * @param	string		$pLogin	login
 	 * @param	string		$pPass	password
 	 * @return	boolean				true if the login is successful or if it is a public resource 
 	 */    
    public function auth($pLogin, $pPass) 
    {
		$db = DbUtil::accessFactory();
		$pLogin = $db->escape($pLogin);
		$pPass = md5($db->escape($pPass));
		
		$rs = $db->select("SELECT id, level FROM users WHERE username = '${pLogin}' AND password = '${pPass}'");
	
		/* Check if a result was found */
		if ($rs->count())
		{
			$this->userid 		= $rs->id;
			$this->userlevel 	= $rs->level;
			
			return true;
		}
		
		return false;
    }
    
 	/**
 	 * list of items (this method is optional)
	 * WARNING: this method should not modify data (REST "Safety" concept)
 	 * GET /item 
 	 * or HEAD /item
 	 * @param	boolean	$pHead	true if we return only headers
 	 */
    public function index($pHead = false) 
    {
    	$db = DbUtil::accessFactory();
    	
		header('Content-type: text/xml');
		
		if ($pHead)
			return;
			
		echo '<?xml version="1.0"?>';
		
		$rs = $db->select("SELECT t.label, COUNT(w.widget) AS 'widgetcount' FROM tags AS t LEFT OUTER JOIN widget_tags AS w ON w.tag = t.label GROUP BY t.label ORDER BY t.label ASC");							
	
		echo '<tags>';
		while($rs->fetch())
		{
			echo '<tag membersCount="'.$rs->widgetcount.'">'.BASE_WS_URL.'tag/'.$rs->label.'</tag>';
		}		
		echo '</tags>';
    }
	/**
 	 * display an item (this method is optional)
	 * WARNING: this method should not modify data (REST "Safety" concept)
 	 * GET /item/id
 	 * or HEAD /item/id
 	 * @param	string		@pId	id of the resource
 	 * @param	boolean 	$pHead	true if we return only headers
 	 */    
    public function show($pId, $pHead = false) 
    {
    	$db = DbUtil::accessFactory();
    	$pId = $db->escape($pId);

		$rs = $db->select("SELECT label FROM tags WHERE label = '${pId}'");
		
		if ($rs->count())
		{		

			header('Content-type: text/xml');  
			  	
			if ($pHead) 
				return;
				
			echo '<?xml version="1.0"?>';		
		
			echo '<tag>';
			echo '<name>'.$rs->label.'</name>';
			echo '<widgets>'.BASE_WS_URL.'tag/'.$rs->label.'/widget</widgets>';
			echo '</tag>';
		} else {
			throw new RESTException("", 404);
		}		
    }
}
?>